<h1>Compétition : <%= @tournament.name %></h1>

<div class="title_w_bg">Format</div>
<%= @tournament.try(:format) %>
<br>

<div class="title_w_bg">Statut</div>
<%= @tournament.finished ? "Terminé" : "Ouvert" %></td>
<%= @tournament.try(:status) == "launched" ? " - En cours" : " - Non commencé"%></td>

<br>
<% if user_signed_in? && current_user.admin %>
  <% if @tournament.finished? %>
    <%= link_to "Réouvrir", open_tournament_path %>
  <% else %>
    <%= link_to "Cloturer", close_tournament_path %>
  <% end %>
<% end %>
<% if user_signed_in? && current_user.admin %>
  <% if @tournament.try(:status) != "lauched" %>
    <br>
    <%= link_to "Lancer le tournoi", close_inscriptions_path %>
  <% end %>
<% end %>

<% if (@tournament.format == "Championnat" || @tournament.format.nil?) %>
  <br><br>
  <div class="title_w_bg">Classement</div>
  <table class="leaderboard">
    <tr>
      <th> </th>
      <th> </th>
      <th>Pts</th>
      <th>J.</th>
      <th>G.</th>
      <th>G.pr</th>
      <th>G.pn.</th>
      <th>N.</th>
      <th>D.</th>
      <th>D.pr</th>
      <th>D.pn.</th>
      <th>bp.</th>
      <th>bc.</th>
      <th class="lb_br">Diff.</th>
    </tr>
    <br>

    <% i = 0
      @tournament.seasons.order(points: :desc).each { |ts|
      i = i+1.to_i
    %>
    <tr>
      <td><%= i %></td>
      <td><%= ts.player.pseudo %></td>
      <td><b><%= ts.points %></b></td>
      <td class="lb_br"><%= ts.teams.count %></td>
      <td><%= ts.win %> </td>
      <td><%= ts.win_prol %> </td>
      <td class="lb_br"><%= ts.win_peno %> </td>
      <td class="lb_br"><%= ts.draw %> </td>
      <td><%= ts.lose %> </td>
      <td><%= ts.lose_prol %> </td>
      <td class="lb_br"><%= ts.lose_peno %> </td>
      <td><%= @bp = Team.all.where("season_id= '#{ts.id}'").sum(:score) %></td>
      <td><%= @bc = (Team.where("season_id= '#{ts.id}'").collect{|a| a.match.teams.collect{|b| b.score}.sum }.sum)-(Team.all.where("season_id= '#{ts.id}'").sum(:score)) %></td>
      <td class="lb_br"><%= @bp-@bc %> </td>

    </tr>
    <% } %>
  </table>

  <div class="title_w_bg">Statistiques : Ratios par match </div>

  <table class="leaderboard">
    <tr>
      <th> </th>
      <th> </th>
      <th>Pts / match</th>
      <th>Matchs<br>joués</th>
      <th>G.</th>
      <th>G.pr</th>
      <th>G.pn.</th>
      <th>N.</th>
      <th>D.</th>
      <th>D.pr</th>
      <th>D.pn.</th>
      <th class="lb_br">Buts /<br>match</th>
      <th class="lb_br">Bc./<br>match</th>
    </tr>
    <br>

    <% i = 0
      @tournament.seasons.order(points: :desc).each do |ts|
      i = i+1.to_i
    %>
      <tr>
        <% if @tournament.matches.count != 0 %>
        <td><%= i %></td>
        <td class="lead_team"><%= ts.player.pseudo %></td>
        <td class="lb_br"><b><%= (ts.points.to_f/ ts.teams.count).round(2)%></b></td>
        <td class="lb_br"><%= ts.teams.count %> </td>
          <% if ts.teams.count != 0 %>
          <td>              <%= number_to_percentage(((100 * ts.win) / ts.teams.count), precision: 0) %> </td>
          <td>              <%= number_to_percentage(((100 * ts.win_prol)/ ts.teams.count), precision: 0) %> </td>
          <td class="lb_br"><%= number_to_percentage(((100 * ts.win_peno) / ts.teams.count), precision: 0) %> </td>
          <td class="lb_br"><%= number_to_percentage(((100 * ts.draw) / ts.teams.count), precision: 0) %> </td>
          <td>              <%= number_to_percentage(((100 * ts.lose) / ts.teams.count), precision: 0) %> </td>
          <td>              <%= number_to_percentage(((100 * ts.lose_prol) / ts.teams.count), precision: 0) %> </td>
          <td class="lb_br"><%= number_to_percentage(((100 * ts.lose_peno) / ts.teams.count), precision: 0) %> </td>
          <td><%= (Team.all.where("season_id= '#{ts.id}'").sum(:score).to_f / ts.teams.count).round(2) %></td>
          <td class="lb_br"><%= (((Team.where("season_id= '#{ts.id}'").collect{|a| a.match.teams.collect{|b| b.score}.sum }.sum)-(Team.all.where("season_id= '#{ts.id}'").sum(:score))).to_f / ts.teams.count).round(2) %></td>
          <% end %>
        <% end %>


      </tr>
    <% end %>

  </table>
<% end %>

<br>
<% if @tournament.format == "Coupe" %>
  <div class="title_w_bg">Liste des joueurs</div>
  <br>
  <% @tournament.players.all.each{ |p| %>
    <%= p.pseudo %>
    <br>
  <% } %>

  <div class="title_w_bg">Tableau</div>
  <% if @tournament.status == "launched" %>
    <div class="cup_round">
      <div><%= @tournament.rounds.first.step %></div>


      <div><%= @tournament.seasons.count %></div>
      <br><br>
      <% @tournament.seasons.order(:seat).all.each_slice(2){ |ss| %>
        <div class="cup_match">
          <% ss.each { |s| %>
          <div class="cup_team"><%= "Siège #{s.seat} #{s.player.try(:pseudo)}" %></div>
          <% } %>
          <div class="match_link">Match
          </div>
        </div>
      <% } %>
    </div>
  <% else %>
    <div><%= @tournament.rounds.first.step %></div>
    <div><% @tournament.rounds.first.seasons.each { |rs| %>
      <%= rs.id %>
    <% } %>
    </div>
    <br>
    <% @tournament.seasons.all.each{ |s| %>
      <div class="cup_team"><%= s.init_seat.nil? ? "Pas de siège défini" :  "Siège #{s.init_seat} #{s.player.try(:pseudo)} #{s.rounds.first.try(:step)}" %></div>
    <% } %>
  <% end %>
<% end %>

<br><br>
<div class="title_w_bg">Matchs</div>
<br>
<table class="scoreboard">

  <tr>
    <th>ID du match</th>
    <th>Mode</th>
    <th>Joueur 1</th>
    <th>Team 1</th>
    <th>Score</th>
    <th>Team 2</th>
    <th>Joueur 2</th>

  </tr>
  <br>

  <% if !@tournament.matches.empty? %>
    <% @tournament.matches.order('id DESC').each do |match| %>

      <tr>
        <td><%= match.id %></td>
        <td><%= match.mode %></td>
        <td><%= link_to Player.find(match.teams.first.player_id).pseudo, player_path(match.teams.first.player_id) %> </td>
        <td><%= match.teams.first.club.name %> </td>
        <td><div class="tab_score">
              <%= match.teams.first.score %> - <%= match.teams.last.score %>
            </div>
            <span class="tab_score_prol">
            <%= (match.prolongations && !match.teams.first.prol_score) ? "a.p" : "" %>
            <%= match.teams.first.prol_score %><%= match.teams.first.prol_score == nil ? '' : ' tab ' %><%= match.teams.last.prol_score %>
            </span>
        </td>

        <td><%= match.teams.last.club.name %> </td>
        <td><%= link_to Player.find(match.teams.last.player_id).pseudo, player_path(match.teams.last.player_id) %> </td>
        <td><%= link_to "Détails", match_path(match)%></td>
      </tr>
      <% end %>

  <% else %>
    Pas de match pour ce tournoi
  <% end %>

</table>
<br>

<% if (@tournament.format == "Championnat" || @tournament.format.nil?) %>

  <div class="title_w_bg">Règles</div>
  <br>Points pour une victoire :
  <br>Sur 90mn : <%= @tournament.win_regular_points %>
  <br>Après prolongations : <%= @tournament.win_prol_points %>
  <br>Aux pénalties : <%= @tournament.win_peno_points %>
  <br><br>
  Points pour une défaite
  <br>90mn : <%= @tournament.lose_regular_points %>
  <br>Après prolongations : <%= @tournament.lose_prol_points %>
  <br>Aux pénaltys : <%= @tournament.lose_peno_points %>

  <br><br>Points pour un match nul : <%= @tournament.draw_regular_points %>

<% end %>

<br><br>
<% if user_signed_in? %>
<%= link_to "Modifier", edit_tournament_path(@tournament)%></td>
<br><br>
<%= link_to 'Supprimer', tournament_path(@tournament), method: :delete, data: { confirm: 'Etes vous sur de supprimer cette compétition et tous ses résultats ?' }%>
<br><br>
<% end %>
<%= link_to "Retour à la liste des compétitions", all_tournaments_path %>
